package src;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class CustomerQueryDialog extends JDialog {
    private JTable customerTable;
    private DefaultTableModel tableModel;
    private JTextField searchField;
    private JComboBox<String> searchTypeCombo;

    public CustomerQueryDialog(Frame owner) {
        super(owner, "客户查询", true);
        setSize(800, 600);
        setLocationRelativeTo(owner);

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());

        // 创建搜索面板
        JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        searchTypeCombo = new JComboBox<>(new String[]{"客户编号", "客户姓名", "身份证号", "联系电话"});
        searchField = new JTextField(20);
        JButton searchButton = new JButton("搜索");
        JButton resetButton = new JButton("重置");

        searchPanel.add(new JLabel("搜索类型:"));
        searchPanel.add(searchTypeCombo);
        searchPanel.add(new JLabel("搜索内容:"));
        searchPanel.add(searchField);
        searchPanel.add(searchButton);
        searchPanel.add(resetButton);

        // 创建表格
        String[] columnNames = {"客户编号", "客户姓名", "身份证号", "联系电话", "联系地址"};
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        customerTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(customerTable);

        // 添加事件监听器
        searchButton.addActionListener(e -> performSearch());
        resetButton.addActionListener(e -> {
            searchField.setText("");
            refreshTable();
        });

        // 添加组件到主面板
        mainPanel.add(searchPanel, BorderLayout.NORTH);
        mainPanel.add(scrollPane, BorderLayout.CENTER);

        // 添加关闭按钮
        JButton closeButton = new JButton("关闭");
        closeButton.addActionListener(e -> dispose());
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.add(closeButton);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(mainPanel);

        // 初始加载数据
        refreshTable();
    }

    private void performSearch() {
        String searchType = (String) searchTypeCombo.getSelectedItem();
        String searchContent = searchField.getText().trim();
        
        // TODO: 实现实际的搜索逻辑
        // 这里只是示例，实际应该从数据库查询
        tableModel.setRowCount(0);
        if (!searchContent.isEmpty()) {
            // 模拟搜索结果
            Object[] row = {"001", "张三", "110101199001011234", "13800138000", "北京市朝阳区"};
            tableModel.addRow(row);
        }
    }

    private void refreshTable() {
        // TODO: 实现从数据库加载所有客户数据
        tableModel.setRowCount(0);
        // 模拟数据
        Object[][] data = {
            {"001", "张三", "110101199001011234", "13800138000", "北京市朝阳区"},
            {"002", "李四", "110101199002022345", "13800138001", "北京市海淀区"}
        };
        for (Object[] row : data) {
            tableModel.addRow(row);
        }
    }
} 